import Search from "../../components/Search/Search";
import {
  reqgetbanner,
  reqgetcate,
  reqgetcountdown,
  reqgetgroupon,
  reqgetnew,
  reqgetseckill,
} from "../../http/api";
//初始值
const initState = {
  banner: [],
  list: [],
  newList: [],
  moreList: [],
  timeList: [],
  seckList: [],
  key: "",
};

//types
const TYPES = {
  HOME_CHANGE_BANNER: "HOME_CHANGE_BANNER",
  HOME_CHANGE_LIST: "HOME_CHANGE_LIST",
  HOME_CHANGE_NEWLIST: "HOME_CHANGE_NEWLIST",
  HOME_CHANGE_MORELIST: "HOME_CHANGE_MORELIST",
  HOME_CHANGE_TIMELIST: "HOME_CHANGE_TIMELIST",
  HOME_CHANGE_SECKLIST: "HOME_CHANGE_SECKLIST",
  HOME_CHANGE_KEY: "HOME_CHANGE_KEY",
};

//修改state的reducer
const reducer = (state = initState, action) => {
  switch (action.type) {
    case TYPES.HOME_CHANGE_BANNER:
      return {
        ...state,
        banner: action.banner,
      };
    case TYPES.HOME_CHANGE_LIST:
      return {
        ...state,
        list: action.list,
      };
    case TYPES.HOME_CHANGE_NEWLIST:
      return {
        ...state,
        newList: action.newList,
      };
    case TYPES.HOME_CHANGE_MORELIST:
      return {
        ...state,
        moreList: action.moreList,
      };
    case TYPES.HOME_CHANGE_TIMELIST:
      return {
        ...state,
        timeList: action.timeList,
      };
    case TYPES.HOME_CHANGE_SECKLIST:
      return {
        ...state,
        seckList: action.seckList,
      };
    case TYPES.HOME_CHANGE_KEY:
      return {
        ...state,
        key: action.key,
      };
    default:
      return state;
  }
};

//actionCreators
export const actions = {
  changeBanner: (banner) => ({
    type: TYPES.HOME_CHANGE_BANNER,
    banner,
  }),
  //请求轮播
  reqBanner: () => {
    return (dispatch, getState) => {
      let {
        home: { banner },
      } = getState();
      if (banner.length > 0) {
        return;
      }
      reqgetbanner().then((res) => {
        if (res.data.code == 200) {
          dispatch(actions.changeBanner(res.data.list));
        }
      });
    };
  },

  changeList: (list) => ({
    type: TYPES.HOME_CHANGE_LIST,
    list,
  }),
  //请求分类
  reqList: () => {
    return (dispatch, getState) => {
      let {
        home: { list },
      } = getState();
      if (list.length > 0) {
        return;
      }
      reqgetcate().then((res) => {
        if (res.data.code == 200) {
          dispatch(actions.changeList(res.data.list));
        }
      });
    };
  },

  changeNewList: (newList) => ({
    type: TYPES.HOME_CHANGE_NEWLIST,
    newList,
  }),
  //请求新人专享
  reqNewList: () => {
    return (dispatch, getState) => {
      let {
        home: { newList },
      } = getState();
      if (newList.length > 0) {
        return;
      }
      reqgetnew().then((res) => {
        if (res.data.code == 200) {
          dispatch(actions.changeNewList(res.data.list));
        }
      });
    };
  },

  changeMoreList: (moreList) => ({
    type: TYPES.HOME_CHANGE_MORELIST,
    moreList,
  }),
  //请求万人团
  reqMoreList: () => {
    return (dispatch, getState) => {
      let {
        home: { moreList },
      } = getState();
      if (moreList.length > 0) {
        return;
      }
      reqgetgroupon().then((res) => {
        dispatch(actions.changeMoreList(res.data.list));
      });
    };
  },

  changeTimeList: (timeList) => ({
    type: TYPES.HOME_CHANGE_TIMELIST,
    timeList,
  }),
  //请求倒计时
  reqTimeList: () => {
    return (dispatch, getState) => {
      let {
        home: { timeList },
      } = getState();
      if (timeList.length > 0) {
        return;
      }
      reqgetcountdown().then((res) => {
        if (res.data.code == 200) {
          dispatch(actions.changeTimeList(res.data.list?res.data.list:[]));
        }
      });
    };
  },

  changeSeckList: (seckList) => ({
    type: TYPES.HOME_CHANGE_SECKLIST,
    seckList,
  }),
  //请求秒杀
  reqSeckList: () => {
    return (dispatch, getState) => {
      let {
        home: { seckList },
      } = getState();
      if (seckList.length > 0) {
        return;
      }
      reqgetseckill().then((res) => {
        if (res.data.code == 200) {
          dispatch(actions.changeSeckList(res.data.list));
        }
      });
    };
  },
  changeKey: (key) => ({
    type: TYPES.HOME_CHANGE_KEY,
    key,
  }),
  
  //切换分类
  change:(e,push)=>{
    return (dispatch,getState)=>{
        let {home:{list}} = getState();
        push("/index/cate?fid="+list[e-1].id);
    }
  },
  //获取搜索框的值
  getValue:(e)=>{
      return (dispatch,getState)=>{
          dispatch(actions.changeKey(e.target.value))
      }
  },
  //点击搜索按钮
  search:(push)=>{
      return (dispatch,getState)=>{
          let {home:{key}} = getState();
          push("/search?key=" + key);
      }
  }
};

//导出数据
export const getBanner = (state) => state.home.banner;
export const getList = (state) => state.home.list;
export const getNewList = state =>state.home.newList
export const getMoreList = state=>state.home.moreList
export const getTimeList = state =>state.home.timeList
export const getSeckList = state =>state.home.seckList;
export const getKey = state=>state.home.key

//导出reducer
export default reducer;
